package com.fjzx.frame.page;

import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.fjzx.androidframe.base.activity.BaseSimpleActivity;
import com.fjzx.androidframe.base.adapter.BaseFragmentPagerAdapter;
import com.fjzx.androidframe.base.annotation.ContentView;
import com.fjzx.frame.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author xiongyongcong
 */
@ContentView(R.layout.activity_main)
public class MainActivity extends BaseSimpleActivity {
    @BindView(R.id.bnv_navi)
    BottomNavigationView bnvNavi;
    @BindView(R.id.vp_pager)
    ViewPager vpPager;

    @Override
    protected void initView() {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new CategoryFragment());
        fragments.add(new ProjectFragment());

        vpPager.setAdapter(new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                bnvNavi.getMenu().getItem(position).setChecked(true);
            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageScrollStateChanged(int state) { }
        });

        bnvNavi.setOnNavigationItemSelectedListener(item -> {
            int id = item.getItemId();
            switch (id) {
                case R.id.menu_home:
                    vpPager.setCurrentItem(0);
                    break;
                case R.id.menu_category:
                    vpPager.setCurrentItem(1);
                    break;
                case R.id.menu_project:
                    vpPager.setCurrentItem(2);
                    break;
            }
            return true;
        });
    }
}
